#########################################
# Setup
#########################################

rm(list = ls())
setwd(".")
# Libraries
library(tidyverse)
library(modelsummary)
library(ggpubr)

# Read data
survey <- read.csv("survey_data.csv") 


# Subset survey data to incumbent-aligned & opposition-aligned conditions
survey_inc <- survey %>% filter(condition %in% c(1, 2, 3, 4)) # incumbent-aligned
survey_opp <- survey %>% filter(condition %in% c(5, 6, 7, 8)) # opposition-aligned

# Clean survey data
survey_inc <- survey_inc %>%
  # Recode NA (non-adjusted) beliefs to prior
  mutate(prop_belief = case_when(is.na(prop_belief) == T ~ 50,
                                 .default = prop_belief),
         opp_belief = case_when(is.na(opp_belief) == T ~ 80,
                                .default = opp_belief)) %>%
  # Encode treatment conditions
  mutate(reform = case_when(condition %in% c(1,3) ~ "1_reform",
                            condition %in% c(2,4) ~ "0_sq"),
         reform_aligned = case_when(condition == 2 ~ "0_ra",
                                    condition == 4 ~ "1_ra",
                                    condition == 1 ~ "2_ra",
                                    condition == 3 ~ "3_ra"))

  
survey_opp <- survey_opp %>%
  # Recode NA (non-adjusted) beliefs to prior
  mutate(prop_belief = case_when(is.na(prop_belief) == T ~ 50,
                                 .default = prop_belief),
         opp_belief = case_when(is.na(opp_belief) == T ~ 80,
                                .default = opp_belief)) %>%
  # Encode treatment conditions
  mutate(reform = case_when(condition %in% c(5,7) ~ "1_reform",
                            condition %in% c(6,8) ~ "0_sq"),
         reform_aligned = case_when(condition == 6 ~ "0_ra",
                                    condition == 8 ~ "1_ra",
                                    condition == 5 ~ "2_ra",
                                    condition == 7 ~ "3_ra")) 


#########################################
# Figure 1
#########################################

plot.new()

p1 <- survey_inc %>% 
  group_by(reform) %>%
  filter(mvc_correct == 2) %>%
  summarize(mean = mean(prop_belief, na.rm = T)/100,
            n = n(), 
            sd = sd(prop_belief, na.rm=TRUE)/100,
            se = sd / sqrt(n),
            ci_lower = mean - qt(0.975, df = n-1) * se,
            ci_upper = mean + qt(0.975, df = n-1) * se) %>%
  ungroup() %>%
  mutate(bayes = c(0.489,0.6)) %>% 
  ggplot(aes(reform, mean)) +
  geom_bar(stat = "identity", width = 0.3, fill = "darkgrey") +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1) +
  geom_point(size = 3, color = "black") +
  theme_bw() +
  geom_bar(aes(reform, bayes),stat = "identity",
           alpha = 0.3, width = 0.3, fill = NA, color = "black") +
  ylim(0,1) +
  xlab("Experimental Condition") +
  ylab("Mean Posterior Belief") +
  ggtitle("Mean Beliefs on Incumbent Competence") +
  scale_x_discrete(labels = c("SQ (Bad News)", "Reform (Good News)")) +
  geom_hline(aes(yintercept = 0.5), linetype = "dashed")

p2  <-  survey_inc %>% 
  group_by(reform) %>%
  filter(mvc_correct == 2) %>%
  summarize(mean = mean(opp_belief, na.rm = T)/100,
            n = n(),
            sd = sd(opp_belief, na.rm=TRUE)/100,
            se = sd / sqrt(n),
            ci_lower = mean - qt(0.975, df = n-1) * se,
            ci_upper = mean + qt(0.975, df = n-1) * se) %>%
  ungroup() %>%
  mutate(bayes = c(0.89,0)) %>% 
  ggplot(aes(reform, mean)) +
  geom_bar(aes(fill = "Observed\nPosterior"),stat = "identity", width = 0.3) +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1) +
  geom_point(size = 3, color = "black") +
  theme_bw() +
  geom_bar(aes(reform, bayes , color = "Bayesian\nPosterior" ),stat = "identity",
           alpha = 0.3, width = 0.3, fill = NA) +
  ylim(0,1) +
  xlab("Experimental Condition") +
  ylab("Mean Posterior Belief") +
  ggtitle("Mean Beliefs on Opposition Veto Power") +
  scale_x_discrete(labels = c("SQ (Bad News)", "Reform (Good News)")) +
  geom_hline(aes(yintercept = 0.8, linetype = "Prior" )) +
  scale_color_manual(name = "", values = c("black"))+
  scale_linetype_manual(name = "", values = c(2)) +
  scale_fill_manual(name = "", values = c("darkgrey"))+
  theme(
    legend.spacing.y = unit(0, "cm")  # Adjust space between legend items
  )+
  guides(
    linetype = guide_legend(order = 1, title = NULL, override.aes = list(color = "black")),
    color = guide_legend(order = 2, title = NULL, override.aes = list(linetype = 1)),
    fill = guide_legend(order = 3, title = NULL)
  )


fig_1 <- ggarrange(p1, p2, ncol = 2, nrow = 1, 
                           common.legend = TRUE, legend = "bottom")

print(fig_1)


#########################################
# Figure 2
#########################################

plot.new()

p3 <- survey_inc %>% 
  group_by(reform_aligned) %>%
  filter(mvc_correct == 2) %>%
  summarize(mean = mean(prop_belief, na.rm = T)/100,
            n = n(), 
            sd = sd(prop_belief, na.rm=TRUE)/100,
            se = sd / sqrt(n),
            ci_lower = mean - qt(0.975, df = n-1) * se,
            ci_upper = mean + qt(0.975, df = n-1) * se) %>%
  ungroup() %>%
  mutate(bayes = c(0.489, 0.489, 0.6, 0.6)) %>% 
  ggplot(aes(reform_aligned, mean)) +
  geom_bar(stat = "identity", width = 0.3, fill = "darkgrey") +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1) +
  geom_point(size = 3, color = "black") +
  geom_bar(aes(reform_aligned, bayes),stat = "identity",
           alpha = 0.3, width = 0.3, fill = NA, color = "black") +
  theme_bw() +
  ylim(0,1) +
  xlab("Experimental Condition") +
  ylab("Mean Posterior Belief") +
  ggtitle("Mean Beliefs on Incumbent Competence") +
  scale_x_discrete(labels = c("SQ Baseline", "SQ Incumbent",
                              "Reform Baseline", "Reform Incumbent")) +
  geom_hline(aes(yintercept = 0.5), linetype = "dashed") 


p4 <- survey_inc %>% 
  group_by(reform_aligned) %>%
  filter(mvc_correct == 2) %>%
  summarize(mean = mean(opp_belief, na.rm = T)/100,
            n = n(),
            sd = sd(opp_belief, na.rm=TRUE)/100,
            se = sd / sqrt(n),
            ci_lower = mean - qt(0.975, df = n-1) * se,
            ci_upper = mean + qt(0.975, df = n-1) * se) %>%
  ungroup() %>%
  mutate(bayes = c(0.89, 0.89, 0, 0)) %>% 
  ggplot(aes(reform_aligned, mean)) +
  geom_bar(aes(fill = "Observed\nPosterior"), stat = "identity", width = 0.3) +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1) +
  geom_point(size = 3, color = "black") +
  geom_bar(aes(reform_aligned, bayes , color = "Bayesian\nPosterior" ),stat = "identity",
           alpha = 0.3, width = 0.3, fill = NA) +
  theme_bw() +
  ylim(0,1) +
  xlab("Experimental Condition") +
  ylab("Mean Posterior Belief") +
  ggtitle("Mean Beliefs on Opposition Veto Power") +
  scale_x_discrete(labels = c("SQ Baseline", "SQ Incumbent",
                              "Reform Baseline", "Reform Incumbent"))+
  geom_hline(aes(yintercept = 0.8, linetype = "Prior"))  +
  scale_color_manual(name = "", values = c("black"))+
  scale_linetype_manual(name = "", values = c(2)) +
  scale_fill_manual(name = "", values = c("darkgrey"))+
  theme(
    legend.spacing.y = unit(0, "cm")  # Adjust space between legend items
  )+
  guides(
    linetype = guide_legend(order = 1, title = NULL, override.aes = list(color = "black")),
    color = guide_legend(order = 2, title = NULL, override.aes = list(linetype = 1)),
    fill = guide_legend(order = 3, title = NULL)
  )


fig_2 <-  ggarrange(p3, p4, ncol = 2, nrow = 1,
                           common.legend = TRUE, legend = "bottom")
print(fig_2)



#########################################
# Figure 3
#########################################

plot.new()

p5 <- survey_opp %>% 
  group_by(reform) %>%
  filter(mvc_correct == 2) %>%
  summarize(mean = mean(prop_belief, na.rm = T)/100,
            n = n(), 
            sd = sd(prop_belief, na.rm=TRUE)/100,
            se = sd / sqrt(n),
            ci_lower = mean - qt(0.975, df = n-1) * se,
            ci_upper = mean + qt(0.975, df = n-1) * se) %>%
  ungroup() %>%
  mutate(bayes = c(0.489,0.6)) %>% 
  ggplot(aes(reform, mean)) +
  geom_bar(stat = "identity", width = 0.3, fill = "darkgrey") +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1) +
  geom_point(size = 3, color = "black") +
  theme_bw() +
  geom_bar(aes(reform, bayes),stat = "identity",
           alpha = 0.3, width = 0.3, fill = NA, color = "black") +
  ylim(0,1) +
  xlab("Experimental Condition") +
  ylab("Mean Posterior Belief") +
  ggtitle("Mean Beliefs on Incumbent Competence") +
  scale_x_discrete(labels = c("SQ (Good News)", "Reform (Bad News)")) +
  geom_hline(aes(yintercept = 0.5), linetype = "dashed")




p6 <- survey_opp %>% 
  group_by(reform) %>%
  filter(mvc_correct == 2) %>%
  summarize(mean = mean(opp_belief, na.rm = T)/100,
            n = n(),
            sd = sd(opp_belief, na.rm=TRUE)/100,
            se = sd / sqrt(n),
            ci_lower = mean - qt(0.975, df = n-1) * se,
            ci_upper = mean + qt(0.975, df = n-1) * se) %>%
  ungroup() %>%
  mutate(bayes = c(0.89,0)) %>% 
  ggplot(aes(reform, mean)) +
  geom_bar(aes(fill = "Observed\nPosterior"),stat = "identity", width = 0.3) +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1) +
  geom_point(size = 3, color = "black") +
  theme_bw() +
  geom_bar(aes(reform, bayes , color = "Bayesian\nPosterior" ),stat = "identity",
           alpha = 0.3, width = 0.3, fill = NA) +
  ylim(0,1) +
  xlab("Experimental Condition") +
  ylab("Mean Posterior Belief") +
  ggtitle("Mean Beliefs on Opposition Veto Power") +
  scale_x_discrete(labels = c("SQ (Good News)", "Reform (Bad News)")) +
  geom_hline(aes(yintercept = 0.8, linetype = "Prior" )) +
  scale_color_manual(name = "", values = c("black"))+
  scale_linetype_manual(name = "", values = c(2)) +
  scale_fill_manual(name = "", values = c("darkgrey"))+
  theme(
    legend.spacing.y = unit(0, "cm")  # Adjust space between legend items
  )+
  guides(
    linetype = guide_legend(order = 1, title = NULL, override.aes = list(color = "black")),
    color = guide_legend(order = 2, title = NULL, override.aes = list(linetype = 1)),
    fill = guide_legend(order = 3, title = NULL)
  )

fig_3 <- ggarrange(p5, p6, ncol = 2, nrow = 1, 
                           common.legend = TRUE, legend = "bottom")

print(fig_3)



#########################################
# Figure 4
#########################################

plot.new()

p7 <-  survey_opp %>% 
  group_by(reform_aligned) %>%
  filter(mvc_correct == 2) %>%
  summarize(mean = mean(prop_belief, na.rm = T)/100,
            n = n(), 
            sd = sd(prop_belief, na.rm=TRUE)/100,
            se = sd / sqrt(n),
            ci_lower = mean - qt(0.975, df = n-1) * se,
            ci_upper = mean + qt(0.975, df = n-1) * se) %>%
  ungroup() %>%
  mutate(bayes = c(0.489, 0.489, 0.6, 0.6)) %>% 
  ggplot(aes(reform_aligned, mean)) +
  geom_bar(stat = "identity", width = 0.3, fill = "darkgrey") +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1) +
  geom_point(size = 3, color = "black") +
  geom_bar(aes(reform_aligned, bayes),stat = "identity",
           alpha = 0.3, width = 0.3, fill = NA, color = "black") +
  theme_bw() +
  ylim(0,1) +
  xlab("Experimental Condition") +
  ylab("Mean Posterior Belief") +
  ggtitle("Mean Beliefs on Incumbent Competence") +
  scale_x_discrete(labels = c("SQ Baseline", "SQ Opposition",
                              "Reform Baseline", "Reform Opposition")) +
  geom_hline(aes(yintercept = 0.5), linetype = "dashed") 



p8 <-  survey_opp %>% 
  group_by(reform_aligned) %>%
  filter(mvc_correct == 2) %>%
  summarize(mean = mean(opp_belief, na.rm = T)/100,
            n = n(),
            sd = sd(opp_belief, na.rm=TRUE)/100,
            se = sd / sqrt(n),
            ci_lower = mean - qt(0.975, df = n-1) * se,
            ci_upper = mean + qt(0.975, df = n-1) * se) %>%
  ungroup() %>%
  mutate(bayes = c(0.89, 0.89, 0, 0)) %>% 
  ggplot(aes(reform_aligned, mean)) +
  geom_bar(aes(fill = "Observed\nPosterior"), stat = "identity", width = 0.3) +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1) +
  geom_point(size = 3, color = "black") +
  geom_bar(aes(reform_aligned, bayes , color = "Bayesian\nPosterior" ),stat = "identity",
           alpha = 0.3, width = 0.3, fill = NA) +
  theme_bw() +
  ylim(0,1) +
  xlab("Experimental Condition") +
  ylab("Mean Posterior Belief") +
  ggtitle("Mean Beliefs on Opposition Veto Power") +
  scale_x_discrete(labels = c("SQ Baseline", "SQ Opposition",
                              "Reform Baseline", "Reform Opposition"))+
  geom_hline(aes(yintercept = 0.8, linetype = "Prior"))  +
  scale_color_manual(name = "", values = c("black"))+
  scale_linetype_manual(name = "", values = c(2)) +
  scale_fill_manual(name = "", values = c("darkgrey"))+
  theme(
    legend.spacing.y = unit(0, "cm")  # Adjust space between legend items
  )+
  guides(
    linetype = guide_legend(order = 1, title = NULL, override.aes = list(color = "black")),
    color = guide_legend(order = 2, title = NULL, override.aes = list(linetype = 1)),
    fill = guide_legend(order = 3, title = NULL)
  )

fig_4 <-  ggarrange(p7, p8, ncol = 2, nrow = 1,
                           common.legend = TRUE, legend = "bottom")
print(fig_4)


#########################################
# Figure I1
#########################################

p9 <-  survey_inc %>% 
  group_by(reform_aligned) %>%
  filter(congruent == 1)   %>%
  filter(mvc_correct == 2) %>%
  summarize(mean = mean(prop_belief, na.rm = T)/100,
            n = n(), 
            sd = sd(prop_belief, na.rm=TRUE)/100,
            se = sd / sqrt(n),
            ci_lower = mean - qt(0.975, df = n-1) * se,
            ci_upper = mean + qt(0.975, df = n-1) * se) %>%
  ungroup() %>%
  mutate(bayes = c(0.489, 0.489, 0.6, 0.6)) %>% 
  ggplot(aes(reform_aligned, mean)) +
  geom_bar(stat = "identity", width = 0.3, fill = "darkgrey") +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1) +
  geom_point(size = 3, color = "black") +
  geom_bar(aes(reform_aligned, bayes),stat = "identity",
           alpha = 0.3, width = 0.3, fill = NA, color = "black") +
  theme_bw() +
  ylim(0,1) +
  xlab("Experimental Condition") +
  ylab("Mean Posterior Belief") +
  ggtitle("Mean Beliefs on Incumbent Competence") +
  scale_x_discrete(labels = c("SQ Baseline", "SQ Incumbent",
                              "Reform Baseline", "Reform Incumbent")) +
  geom_hline(aes(yintercept = 0.5), linetype = "dashed") 

p10 <-  survey_inc %>% 
  group_by(reform_aligned) %>%
  filter(congruent == 1) %>%
  filter(mvc_correct == 2) %>%
  summarize(mean = mean(opp_belief, na.rm = T)/100,
            n = n(),
            sd = sd(opp_belief, na.rm=TRUE)/100,
            se = sd / sqrt(n),
            ci_lower = mean - qt(0.975, df = n-1) * se,
            ci_upper = mean + qt(0.975, df = n-1) * se) %>%
  ungroup() %>%
  mutate(bayes = c(0.89, 0.89, 0, 0)) %>% 
  ggplot(aes(reform_aligned, mean)) +
  geom_bar(aes(fill = "Observed\nPosterior"), stat = "identity", width = 0.3) +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1) +
  geom_point(size = 3, color = "black") +
  geom_bar(aes(reform_aligned, bayes , color = "Bayesian\nPosterior" ),stat = "identity",
           alpha = 0.3, width = 0.3, fill = NA) +
  theme_bw() +
  ylim(0,1) +
  xlab("Experimental Condition") +
  ylab("Mean Posterior Belief") +
  ggtitle("Mean Beliefs on Opposition Veto Power") +
  scale_x_discrete(labels = c("SQ Baseline", "SQ Incumbent",
                              "Reform Baseline", "Reform Incumbent"))+
  geom_hline(aes(yintercept = 0.8, linetype = "Prior"))  +
  scale_color_manual(name = "", values = c("black"))+
  scale_linetype_manual(name = "", values = c(2)) +
  scale_fill_manual(name = "", values = c("darkgrey"))+
  theme(
    legend.spacing.y = unit(0, "cm")  # Adjust space between legend items
  )+
  guides(
    linetype = guide_legend(order = 1, title = NULL, override.aes = list(color = "black")),
    color = guide_legend(order = 2, title = NULL, override.aes = list(linetype = 1)),
    fill = guide_legend(order = 3, title = NULL)
  )



fig_I1 <-  ggarrange(p9, p10, ncol = 2, nrow = 1,
                           common.legend = TRUE, legend = "bottom")

print(fig_I1)



#########################################
# Figure I2
######################################### 

plot.new()

p11 = survey_opp %>% 
  group_by(reform_aligned) %>%
  filter(congruent == 1)   %>%
  filter(mvc_correct == 2) %>%
  summarize(mean = mean(prop_belief, na.rm = T)/100,
            n = n(), 
            sd = sd(prop_belief, na.rm=TRUE)/100,
            se = sd / sqrt(n),
            ci_lower = mean - qt(0.975, df = n-1) * se,
            ci_upper = mean + qt(0.975, df = n-1) * se) %>%
  ungroup() %>%
  mutate(bayes = c(0.489, 0.489, 0.6, 0.6)) %>% 
  ggplot(aes(reform_aligned, mean)) +
  geom_bar(stat = "identity", width = 0.3, fill = "darkgrey") +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1) +
  geom_point(size = 3, color = "black") +
  geom_bar(aes(reform_aligned, bayes),stat = "identity",
           alpha = 0.3, width = 0.3, fill = NA, color = "black") +
  theme_bw() +
  ylim(0,1) +
  xlab("Experimental Condition") +
  ylab("Mean Posterior Belief") +
  ggtitle("Mean Beliefs on Incumbent Competence") +
  scale_x_discrete(labels = c("SQ Baseline", "SQ Opposition",
                              "Reform Baseline", "Reform Opposition")) +
  geom_hline(aes(yintercept = 0.5), linetype = "dashed") 

p12  <-  survey_opp %>% 
  group_by(reform_aligned) %>%
  filter(congruent == 1) %>%
  filter(mvc_correct == 2) %>%
  summarize(mean = mean(opp_belief, na.rm = T)/100,
            n = n(),
            sd = sd(opp_belief, na.rm=TRUE)/100,
            se = sd / sqrt(n),
            ci_lower = mean - qt(0.975, df = n-1) * se,
            ci_upper = mean + qt(0.975, df = n-1) * se) %>%
  ungroup() %>%
  mutate(bayes = c(0.89, 0.89, 0, 0)) %>% 
  ggplot(aes(reform_aligned, mean)) +
  geom_bar(aes(fill = "Observed\nPosterior"), stat = "identity", width = 0.3) +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1) +
  geom_point(size = 3, color = "black") +
  geom_bar(aes(reform_aligned, bayes , color = "Bayesian\nPosterior" ),stat = "identity",
           alpha = 0.3, width = 0.3, fill = NA) +
  theme_bw() +
  ylim(0,1) +
  xlab("Experimental Condition") +
  ylab("Mean Posterior Belief") +
  ggtitle("Mean Beliefs on Opposition Veto Power") +
  scale_x_discrete(labels = c("SQ Baseline", "SQ Opposition",
                              "Reform Baseline", "Reform Opposition"))+
  geom_hline(aes(yintercept = 0.8, linetype = "Prior"))  +
  scale_color_manual(name = "", values = c("black"))+
  scale_linetype_manual(name = "", values = c(2)) +
  scale_fill_manual(name = "", values = c("darkgrey"))+
  theme(
    legend.spacing.y = unit(0, "cm")  # Adjust space between legend items
  )+
  guides(
    linetype = guide_legend(order = 1, title = NULL, override.aes = list(color = "black")),
    color = guide_legend(order = 2, title = NULL, override.aes = list(linetype = 1)),
    fill = guide_legend(order = 3, title = NULL)
  )


fig_I2 <-  ggarrange(p11, p12, ncol = 2, nrow = 1,
                           common.legend = TRUE, legend = "bottom")

print(fig_I2)

